package com.hxzy.controller.anony.captcha;

import com.hxzy.common.resp.R;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 功能描述
 *
 * @author tonneyyy
 */
@Api(tags = "匿名-验证码")
@RestController
@RequestMapping(value = "/anony/captcha")
public class EasyCaptchaController {

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "获取算术验证码")
    @RequestMapping("/airth")
    public R captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 算术类型
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        captcha.setLen(2);
        String verCode = captcha.text().toLowerCase();
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为30分钟
        redisTemplate.opsForValue().set("captcha:"+key,verCode,5, TimeUnit.MINUTES);

        Map<String, Object> captchaMap = new HashMap<>();
        captchaMap.put("uuid", key);
        captchaMap.put("image", captcha.toBase64());

        // 将key和base64返回给前端
        return  R.success(captchaMap);
    }


}
